<template>
  <el-dialog
    title="配置前置工序"
    :visible.sync="innerVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    top="5vh"
    width="40%"
    @open="openDialog"
    @close="closeDialog"
  >
    <el-form ref="form" v-loading="loading" :model="form" label-width="80px">
      <el-row>
        <el-col :span="16">
          <el-form-item label="前置工序" prop="leadCheckTableId">
            <el-cascader
              v-model="cascadeValue"
              :options="cascadeOptions"
              :props="props"
              :show-all-levels="false"
              clearable
              placeholder="请选择前置工序"
              style="width: 100%"
              @change="handleProcessChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="强制要求" prop="required">
            <el-switch v-model="form.required" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  getPrecondition,
  saveOrUpdatePrecondition
} from '@/api/qualityManage/checkTable'
export default {
  name: 'ProcessPrecondition',
  props: {
    visible: { type: Boolean, default: false },
    labelWidth: { type: String, default: '100px' },
    relateId: { type: String, default: '' },
    processTree: { type: Array, default: _ => [] }
  },
  data() {
    return {
      innerVisible: false,
      loading: false,
      form: {
        id: '',
        leadCheckTableId: '',
        required: false
      },
      cascadeValue: [],
      rules: {
        leadCheckTableId: [
          { required: true, message: '请选择前置工序', trigger: 'blur' }
        ]
      },
      props: {
        expandTrigger: 'hover'
      },
      cascadeOptions: []
    }
  },
  computed: {

  },
  watch: {
    visible(val) {
      this.innerVisible = val
    }
  },
  methods: {
    /**
     * 处理工序改变
     * @param value
     */
    handleProcessChange(value) {
      this.form.leadCheckTableId = value[1]
    },
    /**
     * 窗口打开触发
     */
    openDialog() {
      const checkTableId = this.relateId
      this.form.checkTableId = checkTableId
      this.loading = true
      this.cascadeOptions = this.processTree.map(item => {
        item.children.forEach(item1 => {
          item1.disabled = item1.value === this.relateId
        })
        return item
      })
      getPrecondition(checkTableId).then(res => {
        if (res.code === 200) {
          const data = res.data
          this.form.id = data.id
          this.form.leadCheckTableId = data.leadCheckTableId
          this.form.required = data.required
          const checkType = this.processTree.find(item => {
            return item.children.find(item1 => item1.value === data.leadCheckTableId)
          })
          this.cascadeValue = [checkType.value, data.leadCheckTableId]
        }
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 窗口关闭触发
     */
    closeDialog() {
      this.form.id = ''
      this.cascadeValue = []
      this.$refs.form.resetFields()
      this.$emit('update:visible', false)
    },
    /**
     * 取消操作
     */
    cancel() {
      this.closeDialog()
    },
    /**
     * 确认
     */
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const form = this.form
          form.leadCheckTableId = form.leadCheckTableId || ''
          saveOrUpdatePrecondition(form).then(response => {
            this.$message({ type: 'success', message: '配置成功' })
            this.$emit('change')
            this.closeDialog()
          }).catch(() => {
          }).finally(_ => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>

